import moment from 'moment';
import Studycodemodel from "../mysql_interface/studycodemodel";

interface IJCTimeButton {
  text: string;
  text2: string;
  plain?: boolean;
  size: "large" | "default" | "small";
  type: "primary" | "success" | "warning" | "info" | "danger";
  c: string;
}

export function jianchatime({
  type,
  activityendtime,
  activitystarttime,
  signupendtime,
  signupstarttime,
}: Studycodemodel.Iactivity, time?: number) {
  const b1: IJCTimeButton = {
    text: "即将开赛",
    text2: "未开始",
    plain: true,
    size: "small",
    type: "danger",
    c: "bt-1",
  };
  const b2: IJCTimeButton = {
    text: "立即报名",
    text2: "报名中",
    type: "primary",
    size: "small",
    c: "bt-2",
  };
  const b3: IJCTimeButton = {
    text: "进入比赛",
    text2: "比赛中",
    type: "success",
    size: "small",
    c: "bt-3",
  };
  const b4: IJCTimeButton = {
    text: "赛事结束",
    text2: "已结束",
    type: "info",
    size: "small",
    c: "bt-4",
  };
  // 判断是否在开始之前
  // if (type == 'public') {
  // 即将开赛-未开始
  if (signupstarttime) {
    if (moment(time ? time : new Date).isBefore(new Date(signupstarttime))) {
      return b1
    }
  }
  // 赛事结束-已结束
  if (activityendtime) {
    if (moment(new Date(activityendtime)).isBefore(time ? time : new Date)) {
      return b4
    }
  }
  // 进入比赛-比赛中
  if (activitystarttime && activityendtime) {
    if (moment(time ? time : new Date).isBetween(new Date(activitystarttime), new Date(activityendtime))) {
      return b3;
    }
  }
  // 立即报名-报名中
  if (signupstarttime && signupendtime) {
    if (moment(time ? time : new Date).isBetween(new Date(signupstarttime), new Date(signupendtime))) {
      return b2
    }
  }
  return b1
}

export interface JCTime {
  isok: boolean;
  d: number;
  h: number;
  s: number;
  m: number;
}

export function dhms(activityendtime: string | number | Date, d: number) {
  const time: JCTime = {
    isok: false,
    d: 0,
    h: 0,
    s: 0,
    m: 0,
  };
  let f = d - 1;
  if (activityendtime) {
    f = new Date(activityendtime).getTime();
  } else {
    time.isok = false;
  }
  if (d > f) {
    time.isok = false;
  } else {
    const z = f - d;
    time.isok = true;
    time.d = Math.floor(z / (1000 * 60 * 60 * 24));
    time.h = Math.floor((z - time.d * 1000 * 60 * 60 * 24) / (1000 * 60 * 60));
    time.m = Math.floor(
      (z - time.d * 1000 * 60 * 60 * 24 - time.h * 1000 * 60 * 60) / (1000 * 60)
    );
    time.s = Math.floor(
      (z -
        time.d * 1000 * 60 * 60 * 24 -
        time.h * 1000 * 60 * 60 -
        time.m * 1000 * 60) /
      1000
    );
  }
  return time;
}

export type IALLTYPE = Studycodemodel.IALLTYPE
export const ALLTYPE: IALLTYPE = {
  AC: ["AC", "Accepted"],
  UN: ["UN", "Unaccepted"],
  WA: ["WA", "WrongAnswer"],
  RE: ["RE", "RuntimeError"],
  CE: ["CE", "CompileError"],
  TLE: ['TLE', 'TimeLimitExceeded'],
  MLE: ['MLE', 'MemoryLimitExceeded'],
  // CF
  WT: ['WT', 'Waiting'],
  RJ: ['RJ', 'Rejected'],
};

export function utilresult1(
  esult1: Studycodemodel.Iuserquestion["result"],
  info: Studycodemodel.Iuserquestion["info"]
) {
  if (!esult1) return ALLTYPE.CE;
  if (Array.isArray(info)) {
    const arr = info.filter((v) => {
      if (v.stderr || v.error) return false;
      if (v.result === true) {
        return true;
      }
      if (v.result === false) {
        return false;
      }
    });
    return arr.length === info.length ? ALLTYPE.AC : ALLTYPE.UN;
  } else {
    return ALLTYPE.CE;
  }
}
export function utilresult(v: Studycodemodel.Iuserquestioninfo): Studycodemodel.Iuserquestioninfo['tn'] {
  if (v.stderr || v.error) return ALLTYPE.RE;
  if (v.result === true) {
    return ALLTYPE.AC;
  }
  if (v.result === false) {
    return ALLTYPE.WA;
  }
  if (
    v.output &&
    String(v.output).replace(/(\s+)$/gi, "") ===
    String(v.stdout)?.replace(/(\s+)$/gi, "")
  ) {
    return ALLTYPE.AC;
  } else {
    return ALLTYPE.WA;
  }
}

export function utilresulttype(
  c: Studycodemodel.Iuserquestion | Studycodemodel.Imatchuserquestion
) {
  const { info } = c;
  const question: Studycodemodel.Iquestion | Studycodemodel.Imatchquestion = {};
  if ("question" in c) {
    Object.assign(question, c.question);
  } else if ("matchquestion" in c) {
    Object.assign(question, c.matchquestion);
  }
  const { type } = question;
  let df = 0;
  let result = "错误";

  switch (type) {
    case "判断":
      if (!Array.isArray(info)) {
        const t =
          typeof question?.answer?.answer === "string"
            ? question.answer.answer !== "错误"
            : !!question.answer?.answer;
        if (info?.answer === t) {
          if (info?.result) {
            info.result = true;
            result = "正确";
          }
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
      }
      break;
    case "单选":
      if (!Array.isArray(info)) {
        if (info?.answer === question?.answer?.answer) {
          if (info?.result) {
            info.result = true;
          }
          result = "正确";
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
      }
      break;
    case "填空":
      if (!Array.isArray(info)) {
        if (
          String(info?.answer).replace(/\s*/g, "") ===
          String(question?.answer?.answer).replace(/\s*/g, "")
        ) {
          if (info?.result) {
            info.result = true;
          }
          result = "正确";
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
      }
      break;
    case "多选":
      if (!Array.isArray(info) && Array.isArray(info?.answer)) {
        const msg = info?.answer
          .sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0))
          .join();
        let d: string[] | undefined = [];
        if (Array.isArray(question?.answer?.answer)) {
          d = question?.answer?.answer;
        } else {
          d = new String(question?.answer?.answer).split(",");
        }
        const answer = d
          ?.sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0))
          .join();
        if (msg === answer) {
          if (info?.result) {
            info.result = true;
          }
          result = "正确";
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
      }
      break;
    case "代码":
      if (Array.isArray(info)) {
        const n = info
          .filter((v) => {
            if (v.stderr) return false;
            if (v.result === true) {
              return true;
            }
            if (v.result === false) {
              return false;
            }
            if (
              v.output &&
              String(v.output).replace(/(\s+)$/gi, "") ===
              String(v.stdout)?.replace(/(\s+)$/gi, "")
            ) {
              return true;
            } else {
              return false;
            }
          })
          .map((e) => e.score);
        if (n.length === info.length) {
          result = ALLTYPE.AC[1];
        } else {
          result = ALLTYPE.WA[1];
        }
        const d = n.reduce((a, b) => {
          return Number(a) + Number(b);
        }, 0);

        if ("score" in question) {
          df = Math.floor(Number(question.score) * (Number(d) / 100));
        }
      } else {
        result = ALLTYPE.CE[1];
      }
  }
  return Object.assign(c, {
    question,
    df,
    result,
  }) as unknown as Studycodemodel.Iuserquestion;
}

/**
 *
 * @param c
 * @returns 是否正确 得分 内容 类型
 */
export function utilresulttype2(
  c: Studycodemodel.Iuserquestion | Studycodemodel.Imatchuserquestion
) {
  const { info } = c;
  const question: Studycodemodel.Iquestion | Studycodemodel.Imatchquestion = {};
  if ("question" in c) {
    Object.assign(question, c.question);
  } else if ("matchquestion" in c) {
    Object.assign(question, c.matchquestion);
  }
  const { type } = question;
  let df = 0;
  let result = "错误";
  let context = c.context;
  switch (type) {
    case "判断":
      if (!Array.isArray(info)) {
        const t =
          typeof question?.answer?.answer === "string"
            ? question.answer.answer !== "错误"
            : !!question.answer?.answer;
        if (info?.answer === t) {
          if (info?.result) {
            info.result = true;
            result = "正确";
          }
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
        context = String(info?.answer ? info?.answer : "");
      }
      break;
    case "单选":
      if (!Array.isArray(info)) {
        if (info?.answer === question?.answer?.answer) {
          if (info?.result) {
            info.result = true;
          }
          result = "正确";
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
        context = String(info?.answer ? info?.answer : "");
      }
      break;
    case "填空":
      if (!Array.isArray(info)) {
        if (
          String(info?.answer).replace(/\s*/g, "") ===
          String(question?.answer?.answer).replace(/\s*/g, "")
        ) {
          if (info?.result) {
            info.result = true;
          }
          result = "正确";
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
        context = String(info?.answer ? info?.answer : "");
      }
      break;
    case "多选":
      if (!Array.isArray(info) && Array.isArray(info?.answer)) {
        const msg = info?.answer
          .sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0))
          .join();
        let d: string[] | undefined = [];
        if (Array.isArray(question?.answer?.answer)) {
          d = question?.answer?.answer;
        } else {
          d = new String(question?.answer?.answer).split(",");
        }
        const answer = d
          ?.sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0))
          .join();
        if (msg === answer) {
          if (info?.result) {
            info.result = true;
          }
          result = "正确";
          if ("score" in question) {
            df = df + Number(question.score);
          }
        }
        context = String(info?.answer ? info.answer.join(",") : "");
      }
      break;
    case "代码":
      if (Array.isArray(info)) {
        const n = info
          .filter((v) => {
            if (v.stderr) return false;
            if (v.result === true) {
              return true;
            }
            if (v.result === false) {
              return false;
            }
            if (
              v.output &&
              String(v.output).replace(/(\s+)$/gi, "") ===
              String(v.stdout)?.replace(/(\s+)$/gi, "")
            ) {
              return true;
            } else {
              return false;
            }
          })
          .map((e) => e.score);
        if (n.length === info.length) {
          result = ALLTYPE.AC[1];
        } else {
          result = ALLTYPE.WA[1];
        }
        const d = n.reduce((a, b) => {
          return Number(a) + Number(b);
        }, 0);

        if ("score" in question) {
          df = Math.floor(Number(question.score) * (Number(d) / 100));
        }
      } else {
        result = ALLTYPE.CE[1];
      }
  }
  return {
    result,
    df,
    context,
    type,
  };
}
